﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Net.Mail;
using System.Security.Cryptography;
using System.IO;

namespace Com.ThrongIn.Utilities
{
    public static class CommonFunctions
    {
        public static string Encrypt(this string clearText)
        {
            byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
            string Password = System.Configuration.ConfigurationManager.AppSettings.Get("EncryptDecryptKey");
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
            return Convert.ToBase64String(encryptedData);
        }
        /// </summary>
        /// <param name="clearData">Byte stream to encrypt</param>
        /// <param name="Key">Secret key for symmetric encryption algorithm</param>
        /// <param name="IV">Initialization Vector for symmetric encryption algorithm</param>
        /// <returns>Returns encrypted byte stream (array)</returns>
        public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();
            Rijndael alg = Rijndael.Create();
            alg.Key = Key;
            alg.IV = IV;
            CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(clearData, 0, clearData.Length);
            cs.Close();
            byte[] encryptedData = ms.ToArray();
            return encryptedData;
        }
     
    }
}
